package com.lanqiao.guimei.controller;

import com.lanqiao.guimei.dao.GoodItemDao;
import com.lanqiao.guimei.dao.SaleProdDao;
import com.lanqiao.guimei.vo.GoodItem;
import com.lanqiao.guimei.vo.Product;
import com.lanqiao.web.mvc.annotation.Controller;
import com.lanqiao.web.mvc.annotation.RequestMapping;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author StarsOne
 * @date Create in  2019-8-14 0014 15:12:07
 */
@Controller
public class SaleProdController {


    @RequestMapping("/front/saleprod/search")
    public String showReslut(HttpServletRequest request, HttpSession session) {
        String searchName = request.getParameter("searchName");
        List<GoodItem> goodItems = new GoodItemDao().queryByName(searchName);
        if (goodItems == null || goodItems.size() == 0) {
            return "redirect:/front/noresult.jsp";
        } else {
            session.setAttribute("products_result", goodItems);
            return "redirect:/front/product-list-result.jsp";
        }
    }

    /**
     * front/saleprod/showsType.do?ctgrId=1002
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/front/saleprod/showsType")
    public String showsType(HttpServletRequest request, HttpSession session) {
        String ctgrId = request.getParameter("ctgrId");
        if (ctgrId != null) {
            List<GoodItem> goodItems = new GoodItemDao().queryByCtgrId(Integer.valueOf(ctgrId));
            if (goodItems == null || goodItems.size() == 0) {
                return "redirect:/front/noresult.jsp";
            } else {
                session.setAttribute("products_stype", goodItems);
                return "redirect:/front/product-list-stype.jsp";
            }
        }
        return null;
    }

    /**
     * front/saleprod/showHot.do
     *
     * @param session
     * @return
     */
    @RequestMapping("/front/saleprod/showHot")
    public String showHot(HttpSession session) {
        List<GoodItem> goodItems = new GoodItemDao().queryHot();
        session.setAttribute("product_type", goodItems);
        return "redirect:/front/product-list-hot.jsp";
    }

    @RequestMapping("/front/saleprod/showNew")
    public String showNew(HttpSession session) {
        List<GoodItem> goodItems = new GoodItemDao().queryNew();
        session.setAttribute("products_new", goodItems);
        return "redirect:/front/product-list-new.jsp";
    }

    /**
     * 大分类
     * front/saleprod/showType.do?type=服饰箱包
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/front/saleprod/showType")
    public String showTYpe(HttpServletRequest request, HttpSession session) {

        GoodItemDao goodItemDao = new GoodItemDao();
        String type = request.getParameter("type");
        List<GoodItem> goodItems = goodItemDao.queryByType(type);
        if (goodItems == null || goodItems.size() == 0) {
            return "redirect:/front/noresult.jsp";
        } else {
            session.setAttribute("products_type", goodItems);
            return "redirect:/front/product-list-type.jsp";
        }
    }

    /**
     * 点击商品图片，得到产品数据，重定向商品详情页面
     * front/saleprod/showDetail.do?prodNo=
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/front/saleprod/showDetail")
    public String showDetail(HttpServletRequest request, HttpSession session) {
        String prodNo = request.getParameter("prodNo");
        Product product = new SaleProdDao().queryByProdNo(prodNo);
        session.setAttribute("detail-product", product);
        return "redirect:/front/product-deatil.jsp";
    }

}
